#include <stdlib.h>

#include "lib.h"

#include "slotguts.h"
#include "dddguts.h"


enum
{
    BLANK,
    SEVEN,
    CHERRY,
    JDIAMOND,       // Jackpot
    BAR1,           // Single bar
    DBAR1,          // Diamond, single bar
    UDBAR1,         // Upside down diamond, single bar                       
    BAR2,           // Double bar
    DBAR2,          // Diamond, double bar                       
    UDBAR2,         // Upside down diamond, double bar                       
    DBAR3,          // Diamond, triple bar                       
    BAR3,           // Diamond, triple bar                       
};



enum WinEvents
{
    EVT_LOSER = 0,
    EVT_1CHERRY,
    EVT_2CHERRIES,
    EVT_ANY3BARS,
    EVT_3BAR1,
    EVT_3CHERRIES,
    EVT_2CHERRIES_2x,
    EVT_ANY3BARS_2x,
    EVT_3BAR1_2x,
    EVT_3CHERRIES_2x,
    EVT_3BAR2,
    EVT_3BAR3,
    EVT_3CHERRIES_4x,
    EVT_3BAR1_4x,
    EVT_3BAR2_2x,
    EVT_3BAR3_2x,
    EVT_3SEVENS,
    EVT_3BAR2_4x,
    EVT_3BAR3_4x,
    EVT_3SEVENS_2x,
    EVT_3SEVENS_4x,
    EVT_3JDIAMONDS,
    NUMEVENTS
};

bool8 
DDDSlotGuts::isValidGuts()
{
    return getGutsId() == 200 ||
           getGutsId() == 201 ||
           getGutsId() == 202 ||
           getGutsId() == 203 ||
           getGutsId() == 204 ||
           getGutsId() == 205 ||
           getGutsId() == 206 ||
           getGutsId() == 207;
}

        


void 
DDDSlotGuts::classifyNearMisses(uint8 *stops)
{
    /**
     * Now check for near misses that become hits.
     */
    for ( int i = 0; i < getReelCount(); i++ )
    {
        /**
         * Skip non-blanks
         */

        if ( getIcon(i, stops[i])->iconid == 0 )
        {
            uint8 nextIconid;
            uint8 prevIconid;
            uint8 nextStop = stops[i] + 1;
            uint8 prevStop = stops[i] - 1;

            if ( nextStop >= this->getIconCount(i) )
            {
                nextStop = 0;
            }

            if ( prevStop < 0 )
            {
                prevStop = this->getIconCount(i) - 1;
            }

            nextIconid = getIcon(i, nextStop)->iconid;
            prevIconid = getIcon(i, prevStop)->iconid;

            countUpNearMisses(nextIconid);
            countDownNearMisses(prevIconid);
        }
    }
}
        
void 
DDDSlotGuts::countUpNearMisses(uint8 iconid)
{
    switch ( iconid )
    {
        case UDBAR1:
            m_numBar1++;
            m_numBars++;
            break;

        case UDBAR2:
            m_numBar2++;
            m_numBars++;
            break;
    }

}

void 
DDDSlotGuts::countDownNearMisses(uint8 iconid)
{
    switch ( iconid )
    {
        case DBAR1:
            m_numBar1++;
            m_numBars++;
            break;

        case DBAR2:
            m_numBar2++;
            m_numBars++;
            break;

        case DBAR3:
            m_numBar3++;
            m_numBars++;
            break;
    }

}



void 
DDDSlotGuts::countIcons(uint8 iconid)
{
    switch ( iconid )
    {
        case CHERRY:
            m_numCherries++;
            break;

        case SEVEN:
            m_numSevens++;
            break;

        case BAR1:
        case DBAR1:
        case UDBAR1:
            m_numBar1++;
            m_numBars++;
            break;

        case BAR2:
        case DBAR2:
        case UDBAR2:
            m_numBar2++;
            m_numBars++;
            break;

        case BAR3:
        case DBAR3:
            m_numBar3++;
            m_numBars++;
            break;

        case JDIAMOND:
            m_numJDiamonds++;
            break;
    }

}


uint8
DDDSlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint8 result = EVT_LOSER;

    m_numCherries = 0;
    m_numSevens = 0;
    m_numBar1 = 0;
    m_numBar2 = 0;
    m_numBar3 = 0;
    m_numBars = 0;
    m_numJDiamonds = 0;


    for ( int i = 0; i < getReelCount(); i++ )
    {
        countIcons(getIcon(i, stops[i])->iconid);
    }

    classifyNearMisses(stops);


    //  EVT_3JDIAMONDS ** Jackpot **
    if ( m_numJDiamonds == 3 )
    {
        result = EVT_3JDIAMONDS;
    }

    // EVT_1CHERRY
    // EVT_2CHERRIES
    // EVT_3CHERRIES
    // And the 2x 4x combos
    if ( result == EVT_LOSER && m_numCherries > 0 )
    {
        if ( m_numJDiamonds > 0 )
        {
            if ( m_numJDiamonds == 1 )
            {
                if ( m_numCherries == 1 )
                {
                    result = EVT_2CHERRIES_2x;
                }
                else
                {
                    result = EVT_3CHERRIES_2x;
                }
            }
            else
            {
                result = EVT_3CHERRIES_4x;
            }

        }
        else
        {
            result = m_numCherries == 3 ? EVT_3CHERRIES : (m_numCherries == 2 ? EVT_2CHERRIES : EVT_1CHERRY);
        }
    }

    // EVT_3BAR1
    if ( result == EVT_LOSER && m_numBar1 > 0 )
    {
        if ( m_numJDiamonds > 0 && m_numJDiamonds + m_numBar1 == 3 )
        {
            if ( m_numJDiamonds == 1 )
            {
                result = EVT_3BAR1_2x;
            }
            else
            {
                result = EVT_3BAR1_4x;
            }
        }
        else
        {
            if ( m_numBar1 == 3 )
            {
                result = EVT_3BAR1;
            }
        }
    }

    // EVT_3BAR2
    if ( result == EVT_LOSER && m_numBar2 > 0 )
    {
        if ( m_numJDiamonds > 0 && m_numJDiamonds + m_numBar2 == 3 )
        {
            if ( m_numJDiamonds == 1 )
            {
                result = EVT_3BAR2_2x;
            }
            else
            {
                result = EVT_3BAR2_4x;
            }
        }
        else
        {
            if ( m_numBar2 == 3 )
            {
                result = EVT_3BAR2;
            }
        }
    }

    // EVT_3BAR3
    if ( result == EVT_LOSER && m_numBar3 > 0 )
    {
        if ( m_numJDiamonds > 0 && m_numJDiamonds + m_numBar3 == 3 )
        {
            if ( m_numJDiamonds == 1 )
            {
                result = EVT_3BAR3_2x;
            }
            else
            {
                result = EVT_3BAR3_4x;
            }
        }
        else
        {
            if ( m_numBar3 == 3 )
            {
                result = EVT_3BAR3;
            }
        }
    }

    // EVT_ANY3BARS     -  Must be checked after the bar triplets
    if ( result == EVT_LOSER && m_numBars > 0 )
    {
        if ( m_numJDiamonds == 1 && m_numBars == 2 )
        {
            result = EVT_ANY3BARS_2x;
        }
        else if ( m_numBars == 3 )
        {
            result = EVT_ANY3BARS;
        }
    }

    // EVT_3SEVENS
    if ( result == EVT_LOSER && m_numSevens > 0 )
    {
        if ( m_numJDiamonds > 0 && m_numJDiamonds + m_numSevens == 3 )
        {
            if ( m_numJDiamonds == 1 )
            {
                result = EVT_3SEVENS_2x;
            }
            else
            {
                result = EVT_3SEVENS_4x;
            }
        }
        else
        {
            if ( m_numSevens == 3 )
            {
                result = EVT_3SEVENS;
            }
        }
    }

    return result;
}
